/*
   Test and validation of RDA5807 on ESP32 board.
    
   ATTENTION:  
   Please, avoid using the computer connected to the mains during testing. Used just the battery of your computer. 
   This sketch was tested on ATmega328 based board. If you are not using a ATmega328, please check the pins of your board. 

  | RDA5807    | Function              |ESP LOLIN32 WEMOS (GPIO) |
  |-----------| ----------------------|-------------------------|
  | SDA/SDIO  |   SDIO                |   21 (SDA / GPIO21)     |
  | CLK/CLOCK |   SCLK                |   22 (SCL / GPIO22)     |


   By Ricardo Lima Caratti, 2020.
*/

#include <RDA5807.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

//OLED引脚设置
U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 36, /* data=*/ 35, /* cs=*/ 37, /* dc=*/ 33, /* reset=*/ 34);

// I2C bus pin on ESP32
#define ESP32_I2C_SDA 5
#define ESP32_I2C_SCL 4

#define MAX_DELAY_RDS 40   // 40ms - polling method

long rds_elapsed = millis();

RDA5807 rx;


//按键
int button1Pin = 1;    
int button2Pin = 2;    
int button3Pin = 3;     
int button4Pin = 6;   


// Show current frequency
void showStatus()
{
  char aux[80];
  sprintf(aux,"\nYou are tuned on %u MHz | RSSI: %3.3u dbUv | Vol: %2.2u | %s ",rx.getFrequency(), rx.getRssi(), rx.getVolume(), (rx.isStereo()) ? "Yes" : "No" );
  Serial.print(aux);
}

void setup()
{
    Serial.begin(9600);
    u8g2.begin();
    
    while (!Serial) ;
    
    pinMode(41, OUTPUT);
    pinMode(42, OUTPUT);

    digitalWrite(41, HIGH);
    digitalWrite(42, LOW);

    // The line below may be necessary to setup I2C pins on ESP32
    Wire.begin(ESP32_I2C_SDA, ESP32_I2C_SCL);
    
    rx.setup();

    rx.setVolume(2);

    delay(500);

    // Select a station with RDS service in your place
    Serial.print("\nEstacao 106.5MHz");
    rx.setFrequency(10650); // It is the frequency you want to select in MHz multiplied by 100.

    // Enables SDR
    rx.setRDS(true);

    pinMode(button1Pin, INPUT_PULLUP); 
    pinMode(button2Pin, INPUT_PULLUP);
    pinMode(button3Pin, INPUT_PULLUP);
    pinMode(button4Pin, INPUT_PULLUP);

    
 
}

void loop()
{
  FM_show();


  if (digitalRead(button2Pin) == LOW)
  {
    rx.setFrequencyUp();
  }
  if (digitalRead(button3Pin) == LOW)
  {
    rx.setVolumeUp();
  }
  if (digitalRead(button4Pin) == LOW)
  {
    rx.setVolumeDown();
  }
  /*
  if (digitalRead(button2Pin) == LOW)
  {
    rx.setFrequencyUp();
    char key = Serial.read();
    switch (key)
    {
    case '+':
      rx.setVolumeUp();
      break;
    case '-':
      rx.setVolumeDown();
      break;
    case 'U':
    case 'u':
      rx.setFrequencyUp();
      break;
    case 'D':
    case 'd':
      rx.setFrequencyDown();
      break;
    default:
      break;
    }
    delay(200);
    showStatus();
  } 
  delay(5);
  */
}

void FM_show() {
  char str[64] = {0};
  u8g2.firstPage();
  u8g2.clearBuffer();         //清除内部缓冲区
  u8g2.setFontDirection(0);   //设置字体方向
  
  u8g2.setFont(u8g2_font_ncenB14_tr);     //设置字体
  u8g2.setCursor(15, 20);
  u8g2.print("FM Radio");

  u8g2.setFont(u8g2_font_ncenB10_tf);  
  u8g2.setCursor(15, 40);
  sprintf(str, "%u MHz",rx.getFrequency() );
  u8g2.print(str);
  u8g2.setCursor(15, 55);
  sprintf(str, "Vol: %2.2u",rx.getVolume());
  u8g2.print(str);
  
  
  u8g2.sendBuffer();
  delay(1000);
}
